package org.jeecg.modules.platformMasterEndPoint.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.util.RandomUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.platformMasterEndPoint.entity.JxSiteMasterDo;
import org.jeecg.modules.platformMasterEndPoint.entity.JxSiteMasterVo;
import org.jeecg.modules.platformMasterEndPoint.mapper.JxSiteMasterManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Api(tags = "站长管理")
@RestController
@RequestMapping("/jx/jxSiteMasterManage")
@Slf4j
public class JxSiteMasterManageController {

    @Autowired
    private JxSiteMasterManageMapper jxSiteMasterManageMapper;

    @ApiOperation("添加一位新站长，录入站长身份信息")
    @PostMapping("/addNewSiteMaster")
    public Result<?> addNewSiteMaster(@RequestBody() JxSiteMasterVo siteMasterVo){

        List<JxSiteMasterDo> list = jxSiteMasterManageMapper.selectList(new LambdaQueryWrapper<JxSiteMasterDo>()
                .eq(JxSiteMasterDo::getName, siteMasterVo.getName())
                .eq(JxSiteMasterDo::getIdNum, siteMasterVo.getIdNum())
                .eq(JxSiteMasterDo::getDelFlag, 0));
        if (list != null && list.size() > 0) {
            return Result.error(450, "已存在姓名为: " + siteMasterVo.getName() + ", 身份证号为: " + siteMasterVo.getIdNum() + "的站长，不能添加重复站长");
        }
        JxSiteMasterDo siteMasterDo = new JxSiteMasterDo();
        siteMasterDo.setName(siteMasterVo.getName());
        siteMasterDo.setIdNum(siteMasterVo.getIdNum());
        siteMasterDo.setPhone(siteMasterVo.getPhone());
        siteMasterDo.setWechatNum(siteMasterVo.getWechatNum());
        siteMasterDo.setPassword(RandomUtils.getRandomStr());
        siteMasterDo.setCreateTime(new Date());

        int count = jxSiteMasterManageMapper.insert(siteMasterDo);
        if (count == 1) {
            log.info("录入新站长成功，id为 {}", siteMasterDo.getId());
            HashMap<String, Object> resultData = new HashMap<>();
            resultData.put("id", siteMasterDo.getId());
            return Result.ok(resultData);
        } else {
            return Result.error("录入新站长失败，联系运维人员查看运行日志进行处理");
        }
    }

    @ApiOperation("根据站长id，编辑站长信息")
    @PutMapping("/editSiteMaster")
    public Result<?> editSiteMaster(@RequestParam("id") String id,
                                    @RequestBody() JxSiteMasterVo siteMasterVo) {
        JxSiteMasterDo siteMasterDo = new JxSiteMasterDo();
        siteMasterDo.setName(siteMasterVo.getName());
        siteMasterDo.setIdNum(siteMasterVo.getIdNum());
        siteMasterDo.setPhone(siteMasterVo.getPhone());
        siteMasterDo.setWechatNum(siteMasterVo.getWechatNum());

        int count = jxSiteMasterManageMapper.update(siteMasterDo, new UpdateWrapper<JxSiteMasterDo>().eq("id", id));
        if (count == 1) {
            log.info("编辑站长信息成功，id为 {}", id);
            return Result.ok();
        } else {
            return Result.error("录入新站长失败，联系运维人员查看运行日志进行处理");
        }
    }


    @ApiOperation("根据站长id搜索站长信息")
    @GetMapping("/querySiteMasterById")
    public Result<?> querySiteMasterById(
            @RequestParam(value = "siteMasterId") String siteMasterId) {

        if (siteMasterId != null && !siteMasterId.isEmpty()) {
            JxSiteMasterDo siteMasterDo = jxSiteMasterManageMapper.selectById(siteMasterId);
            if (siteMasterDo != null) {
                JxSiteMasterVo siteMasterVo = new JxSiteMasterVo();
                siteMasterVo.setId(siteMasterDo.getId());
                siteMasterVo.setIdNum(siteMasterDo.getIdNum());
                siteMasterVo.setName(siteMasterDo.getName());
                siteMasterVo.setPhone(siteMasterDo.getPhone());
                siteMasterVo.setWechatNum(siteMasterDo.getWechatNum());
                return Result.ok(siteMasterVo);
            } else {
                return Result.error("不存在此id的站长");
            }
        } else {
            return Result.error("不存在此id的站长");
        }
    }

    @ApiOperation("根据关键字匹配站长姓名，身份号，手机号，微信号 搜索站长信息")
    @GetMapping("/querySiteMaster")
    public Result<?> querySiteMaster(@RequestParam("keyword") String keyword) {

        List<JxSiteMasterDo> list = jxSiteMasterManageMapper.selectSiteMasterByKeyword(keyword);

        List<JxSiteMasterVo> siteMasterBriefInfos = new ArrayList<>();
        for(int i = 0 ; i < list.size() ; i ++) {
            JxSiteMasterDo siteMasterDo = list.get(i);
            JxSiteMasterVo siteMasterVo = new JxSiteMasterVo();
            siteMasterVo.setId(siteMasterDo.getId());
            siteMasterVo.setIdNum(siteMasterDo.getIdNum());
            siteMasterVo.setName(siteMasterDo.getName());
            siteMasterVo.setPhone(siteMasterDo.getPhone());
            siteMasterVo.setWechatNum(siteMasterDo.getWechatNum());

            siteMasterBriefInfos.add(siteMasterVo);
        }

        return Result.ok(siteMasterBriefInfos);
    }

    @ApiOperation("删除站长，仅标记删除")
    @DeleteMapping("/deleteSiteMaster")
    public Result<?> deleteSiteMasterById(@RequestParam("id") String id) {

        int count = jxSiteMasterManageMapper.deleteSiteMasterByDelFlag(id);
        if (count == 1) {
            log.info("删除站长信息成功，id为 {}", id);
            return Result.ok();
        } else {
            return Result.error("删除站长失败，联系运维人员查看运行日志进行处理");
        }
    }
}
